package com.sunyard.utils.wxpayutil;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mazhao
 * @ClassName JsonUtil
 * @Package com.sunyard.util.wxpayutil
 * @Description 使用Jaskson的新API：Streaming API<br>
 * * http://wiki.fasterxml.com/JacksonInFiveMinutes<br>
 * * http://wiki.fasterxml.com/JacksonStreamingApi
 * @Date 2018/11/29 10:56
 * @Version V1.0
 */
public class JsonUtil {

    /**
     * @FIELD: TODO
     */
    public static final ObjectMapper mapper = new ObjectMapper();
    /**
     * @FIELD: TODO
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * @FIELD: TODO
     */
    public static final String DEFAULT_CHARSET = "UTF-8";
    /**
     * @FIELD: TODO
     */
    public static final String EMPTY_JSON = "{}";
    /**
     * @FIELD: TODO
     */
    public static final byte[] EMPTY_JSON_BYTES = new byte[]{'{', '}'};

    static {
        mapper.getSerializationConfig().withDateFormat(new SimpleDateFormat(DATE_FORMAT));
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * @param json
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String json, Class<T> t) {
        if (json == null) {
            return null;
        }
        try {
            return mapper.readValue(json, t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param json
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T fromJsonWithException(String json, Class<T> t) {
        try {
            return mapper.readValue(json, t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param map
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T fromMap(Map<?, ?> map, Class<T> t) {

        if (map == null) {
            return null;
        }
        try {
            return mapper.readValue(toJson(map), t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EMPTY_JSON;
    }

    /**
     * @param obj
     * @return
     */
    public static String toJsonNotNull(Object obj) {
        try {
            ObjectMapper mapper1 = new ObjectMapper();
            mapper1.setSerializationInclusion(Inclusion.NON_NULL);
            return mapper1.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EMPTY_JSON;
    }

    /**
     * @param obj
     * @return
     */
    public static byte[] toJsonBytes(Object obj) {
        try {
            return mapper.writeValueAsBytes(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return EMPTY_JSON_BYTES;
    }

    /**
     * json字符串的格式化
     * 需要格式的json串
     *
     * @param json
     * @param fillStringUnit 每一层之前的占位符号比如空格 制表符
     * @return
     * @author peiyuxin
     */
    public static String formatJson(String json, String fillStringUnit) {
        if (json == null || json.trim().length() == 0) {
            return null;
        }

        int fixedLenth = 0;
        List<String> tokenList = new ArrayList<String>();
        {
            String jsonTemp = json;
            // 预读取
            while (jsonTemp.length() > 0) {
                String token = getToken(jsonTemp);
                jsonTemp = jsonTemp.substring(token.length());
                token = token.trim();
                tokenList.add(token);
            }
        }


        for (int i = 0; i < tokenList.size(); i++) {
            String token = tokenList.get(i);
            int length = token.getBytes().length;
            if (length > fixedLenth && i < tokenList.size() - 1 && ":".equals(tokenList.get(i + 1))) {
                fixedLenth = length;
            }
        }

        StringBuilder buf = new StringBuilder();
        int count = 0;
        for (int i = 0; i < tokenList.size(); i++) {

            String token = tokenList.get(i);

            if (",".equals(token)) {
                buf.append(token);
                doFill(buf, count, fillStringUnit);
                continue;
            }
            if (":".equals(token)) {
                buf.append(" ").append(token).append(" ");
                continue;
            }
            if ("{".equals(token)) {
                String nextToken = tokenList.get(i + 1);
                if ("}".equals(nextToken)) {
                    i++;
                    buf.append("{ }");
                } else {
                    count++;
                    buf.append(token);
                    doFill(buf, count, fillStringUnit);
                }
                continue;
            }
            if ("}".equals(token)) {
                count--;
                doFill(buf, count, fillStringUnit);
                buf.append(token);
                continue;
            }
            if ("[".equals(token)) {
                String nextToken = tokenList.get(i + 1);
                if ("]".equals(nextToken)) {
                    i++;
                    buf.append("[ ]");
                } else {
                    count++;
                    buf.append(token);
                    doFill(buf, count, fillStringUnit);
                }
                continue;
            }
            if ("]".equals(token)) {
                count--;
                doFill(buf, count, fillStringUnit);
                buf.append(token);
                continue;
            }

            buf.append(token);
            // 左对齐
            if (i < tokenList.size() - 1 && ":".equals(tokenList.get(i + 1))) {
                int fillLength = fixedLenth - token.getBytes().length;
                if (fillLength > 0) {
                    for (int j = 0; j < fillLength; j++) {
                        buf.append(" ");
                    }
                }
            }
        }
        return buf.toString();
    }

    private static String getToken(String json) {
        StringBuilder buf = new StringBuilder();
        boolean isInYinHao = false;
        while (json.length() > 0) {
            String token = json.substring(0, 1);
            json = json.substring(1);

            if (!isInYinHao && (":".equals(token) || "{".equals(token) || "}"
                    .equals(token) || "[".equals(token) || "]".equals(token) || ",".equals(token))) {
                if (buf.toString().trim().length() == 0) {
                    buf.append(token);
                }
                break;
            }

            if ("\\".equals(token)) {
                buf.append(token);
                buf.append(json.substring(0, 1));
                json = json.substring(1);
                continue;
            }
            if ("\"".equals(token)) {
                buf.append(token);
                if (isInYinHao) {
                    break;
                } else {
                    isInYinHao = true;
                    continue;
                }
            }
            buf.append(token);
        }
        return buf.toString();
    }

    /**
     * @param buf
     * @param count
     * @param fillStringUnit
     */
    private static void doFill(StringBuilder buf, int count, String fillStringUnit) {
        buf.append("\n");
        for (int i = 0; i < count; i++) {
            buf.append(fillStringUnit);
        }
    }

}
